package or.exa.cangguan.service.impl;

import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import or.exa.cangguan.common.Page;
import or.exa.cangguan.entity.dto.GoodsDto;
import or.exa.cangguan.entity.dto.InOperationDto;
import or.exa.cangguan.entity.po.Goods;
import or.exa.cangguan.entity.po.GoodsPlace;
import or.exa.cangguan.entity.po.InOperation;
import or.exa.cangguan.entity.po.InOperationDetail;
import or.exa.cangguan.entity.vo.GoodsVo;
import or.exa.cangguan.entity.vo.InOperationVo;
import or.exa.cangguan.mapper.*;
import or.exa.cangguan.service.InOperationService;
import or.exa.cangguan.service.InStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;


@Slf4j
@Service
public class InOperationServiceImpl implements InOperationService {
    @Autowired
    private InOperationMapper inOperationMapper;
    @Autowired
    private InOperationDetailMapper inOperationDetailMapper;
    @Autowired
    private LoadingPositionMapper loadingPositionMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private ShelfMapper shelfMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private InStoreService inStoreService;
    @Autowired
    private InStoreMapper inStoreMapper;
    @Autowired
    private GoodsPlaceMapper goodsPlaceMapper;
    
    


    //查询
    @Override
    public Page searchInOperation(InOperationVo inOperationVo) {

        Page page = new Page();
        BeanUtil.copyProperties(inOperationVo, page);

        List<InOperationDto> inOperationDtos = inOperationMapper.selectByPrimaryKey(inOperationVo,page);
        page.setResultList(inOperationDtos);

        Integer totalNum = inOperationMapper.selectCount(inOperationVo);

        page.setTotalNum(totalNum);

        log.info(inOperationDtos.toString());


        return page;
    }


    //添加入库作业单
    @Override
    public Integer addInOperation(InOperationVo inOperationVo) {
        Date date = new Date(System.currentTimeMillis());
        InOperation inOperation = new InOperation();
        BeanUtil.copyProperties(inOperationVo, inOperation);
        inOperation.setCreateTime(date);
        inOperation.setCreateBy(0);
        inOperation.setUpdateBy(0);
        inOperation.setUpdateTime(date);
        String code = loadingPositionMapper.selectByPrimaryKey(inOperationVo.getLoadName());
        inOperation.setLoadCode(code);
        log.info("-------------------------------------"+inOperation);

        log.info("-------------------------------------"+inOperationVo);
        Integer exeWorker = employeeMapper.selectByName(inOperationVo.getExeWorkerName());
        inOperation.setExeWorker(exeWorker);
        log.info("-------------------------------------"+inOperation);

        int i = 0;
         i = inOperationMapper.updateByPrimaryKeySelective(inOperation);

        List<Goods> goods = goodsMapper.selectByName(inOperationVo.getGoodsList());
        int detailCount = 0;
        for (GoodsVo goodsVo : inOperationVo.getGoodsList()) {
            InOperationDetail inOperationDetail = new InOperationDetail();
            inOperationDetail.setCreateTime(date);
            inOperationDetail.setCreateBy(0);
            inOperationDetail.setUpdateBy(0);
            inOperationDetail.setUpdateTime(date);
            inOperationDetail.setShelfId(shelfMapper.selectIdByName(goodsVo.getShelfName()));
            inOperationDetail.setGoodsCount(goodsVo.getGoodsCount());
            inOperationDetail.setInOperationId(inOperation.getId());
            log.info("================================="+inOperationDetail);

            for (Goods good : goods) {
                if (good.getGoodsName().equals(goodsVo.getGoodsName())) {
                    inOperationDetail.setGoodsId(good.getGoodsId());
                    break;
                }

            }
            detailCount+= inOperationDetailMapper.insert(inOperationDetail);



        }


        return i>0&&detailCount==inOperationVo.getGoodsList().size()?1:0;
    }

    //更新作业单
    @Override
    public Integer updateInOperation(InOperationVo inOperationVo) {
        Date date = new Date(System.currentTimeMillis());
        InOperation inOperation = new InOperation();
        BeanUtil.copyProperties(inOperationVo, inOperation);
        inOperation.setUpdateBy(0);
        inOperation.setUpdateTime(date);
        String code=null;
        if(inOperationVo.getLoadName()!=null&& !inOperationVo.getLoadName().isEmpty()){
            code = loadingPositionMapper.selectByPrimaryKey(inOperationVo.getLoadName());
            inOperation.setLoadCode(code);
        }
        Integer exeWorker= null;
        if(inOperationVo.getExeWorkerName()!=null && !inOperationVo.getExeWorkerName().isEmpty()) {
            exeWorker = employeeMapper.selectByName(inOperationVo.getExeWorkerName());
            inOperation.setExeWorker(exeWorker);
        }
        log.info("-------------------------------------"+inOperation);
        int i = inOperationMapper.updateByPrimaryKeySelective(inOperation);
        inOperationDetailMapper.deleteByPrimaryKey(inOperation.getId());

        List<Goods> goods = goodsMapper.selectByName(inOperationVo.getGoodsList());
        int detailCount = 0;
        for (GoodsVo goodsVo : inOperationVo.getGoodsList()) {
            InOperationDetail inOperationDetail = new InOperationDetail();
            inOperationDetail.setCreateTime(date);
            inOperationDetail.setCreateBy(0);
            inOperationDetail.setUpdateBy(0);
            inOperationDetail.setUpdateTime(date);
            inOperationDetail.setShelfId(shelfMapper.selectIdByName(goodsVo.getShelfName()));
            inOperationDetail.setGoodsCount(goodsVo.getGoodsCount());
            inOperationDetail.setInOperationId(inOperation.getId());

            for (Goods good : goods) {
                if (good.getGoodsName().equals(goodsVo.getGoodsName())) {
                    inOperationDetail.setGoodsId(good.getGoodsId());
                    break;
                }

            }
            detailCount+= inOperationDetailMapper.insert(inOperationDetail);



        }


        return i>0&&detailCount==inOperationVo.getGoodsList().size()?1:0;
    }


    //查询货物所放位置
    @Override
    public List<GoodsDto> searchGoods(Integer goodsId) {
        List<GoodsDto> goodsDtos = inOperationDetailMapper.selectByPrimaryKey(goodsId);


        return goodsDtos;

    }



//    更新状态
    @Override
    public Integer updateStatus(Integer inId, String status) {
        if (Objects.equals(status, "2"))
        {
            String status1 = inOperationMapper.selectStatus(inId);
            log.info("status1"+status1);
            if (status1.equals("0")){
                return -1;
            }
            inStoreService.updateStatus(inId,"1",null);
            log.info("状态已更改");
            saveGoods(inId,status);
            log.info("货物已保存");
        }
        if (Objects.equals(status, "1"))
        {

            String name = inOperationMapper.selectByInId(inId);
            if (name==null)
                return 0;
        }
        int i = inOperationMapper.updateStatus(inId,status);

        return i;
    }

    
    //保存货物
    @Override
    public Integer saveGoods(Integer inId, String status) {
        if (Objects.equals(status, "2"))
        {

            Integer id = inOperationMapper.selectIdByInId(inId);
            Integer storeId = inStoreMapper.selectStoreById(inId);
            List<InOperationDetail> inOperationDetails = inOperationDetailMapper.selectByIOId(id);
            log.info("inOperationDetails"+inOperationDetails);
            int flag = 0;
            for (InOperationDetail inOperationDetail : inOperationDetails) {
                flag++;
                GoodsPlace goodsPlace = new GoodsPlace();
                goodsPlace.setStoreId(storeId);
                goodsPlace.setShelfId(inOperationDetail.getShelfId());
                goodsPlace.setGoodsId(inOperationDetail.getGoodsId());
                goodsPlace.setCount(inOperationDetail.getGoodsCount());
                log.info("goodsPlace"+goodsPlace);
                GoodsPlace goodsPlaces = goodsPlaceMapper.selectByCondition(storeId, inOperationDetail.getShelfId(), inOperationDetail.getGoodsId());
                log.info("goodsPlaces"+goodsPlaces);
                if(goodsPlaces!=null){
                    Integer count = goodsPlaceMapper.selectCount(storeId, inOperationDetail.getShelfId(), inOperationDetail.getGoodsId());
                    goodsPlace.setCount(count+inOperationDetail.getGoodsCount());
                    goodsPlace.setId(goodsPlaces.getId());
                    goodsPlaceMapper.updateByPrimaryKeySelective(goodsPlace);


                }
                if (goodsPlaces==null){
                    goodsPlace.setCreateBy(0);
                    goodsPlace.setCreateTime(new Date(System.currentTimeMillis()));
                    goodsPlace.setUpdateBy(0);
                    goodsPlace.setUpdateTime(new Date(System.currentTimeMillis()));
                    log.info("goodsPlace"+goodsPlace);
                    log.info("goodsPlaceStoreId"+goodsPlace.getShelfId());
                    goodsPlaceMapper.insertSelective(goodsPlace);

                }
                log.info("--------------flag"+flag);


            }


        }


            return 0;
    }




}
